﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using TestDataDriver.Properties;

namespace TestDataDriver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string GetFieldTypes(string connectionString, string sql, IDbDriver driver)
        {
            var cnn = driver.CreateConnection();
            cnn.ConnectionString = connectionString;
            cnn.Open();
            var cmd = driver.CreateCommand();
            cmd.CommandText = sql;
            cmd.Connection = cnn;
            var dataReader = cmd.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(driver.ToString());
            sb.AppendLine("==================");
            string format = "type name: {0}, type index: {1}";
            for (int i = 0; i < dataReader.VisibleFieldCount; i++)
            {
                var typeEnum = driver.GetDataReaderSpecificFieldType(dataReader, i);
                sb.AppendLine(string.Format(format, dataReader.GetDataTypeName(i), typeEnum.ToString()));
            }
            sb.AppendLine("==================");
            sb.AppendLine("");
            sb.AppendLine("");
            return sb.ToString();
        }



        private void button_sqlserver_Click(object sender, EventArgs e)
        {
            string sql = "select * from t1";
            var driver = new SqlServerDriver();

            textBox1.Text = textBox1.Text + GetFieldTypes( Settings.Default.SqlServerConnection, sql, driver);
        }



        private void button_postgresql_Click(object sender, EventArgs e)
        {
            string sql = "select * from t1";
            var driver = new PostgresDriver();

            textBox1.Text = textBox1.Text + GetFieldTypes(Settings.Default.PostgresConnection, sql, driver);
        }

        private void button_oracle_Click(object sender, EventArgs e)
        {
            string sql = "select * from t1";
            var driver = new OracleDriver();

            textBox1.Text = textBox1.Text + GetFieldTypes(Settings.Default.OracleConnection, sql, driver);


        }

        private void button_oracle2_Click(object sender, EventArgs e)
        {
            string sql = "select * from t1";
            var driver = new OracleDriver2();

            textBox1.Text = textBox1.Text + GetFieldTypes(Settings.Default.Oracle2Connection, sql, driver);


        }

        private void button_OLEDB_Click(object sender, EventArgs e)
        {
            string sql = "select * from t1";
            var driver = new OleDbDriver();

            textBox1.Text = textBox1.Text + GetFieldTypes(Settings.Default.OleDbConnection, sql, driver);


        }

        private void button_MySQl_Click(object sender, EventArgs e)
        {
            string sql = "select * from t1";
            var driver = new MySqlDriver();

            textBox1.Text = textBox1.Text + GetFieldTypes(Settings.Default.MySqlConnection, sql, driver);

        }
    }
}
